package ua.lviv.franko.solvers.stressanalysis.oneDimention;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import ua.lviv.franko.FEMShapeFunction1D.FEMShapeFunction1D1;
import ua.lviv.franko.FEMShapeFunction1D.IFEM1D;
import ua.lviv.franko.integration.IFunction1D;
import ua.lviv.franko.integration.MatrixFunction1D;
import ua.lviv.franko.integration.VectorFunction1D;
import ua.lviv.franko.integration.VectorFunctionWithoutDerivatives1D;
import ua.lviv.franko.mlsFunctions.oneDimension.BaseFunctionCoupled;
import ua.lviv.franko.mlsFunctions.oneDimension.Particle;
import ua.lviv.franko.mlsFunctions.oneDimension.weightFunctions.Gaussian;
import ua.lviv.franko.mlsFunctions.oneDimension.weightFunctions.IWeightFunction;
import ua.lviv.franko.solvers.oneDimention.HasBaseNode;
import ua.lviv.franko.visualisation.NewOkCancelDialog;

public class EnrichedLinearElementType2 extends AbstractElement1D {

	private int					numOfMFBaseFunctions;
	// EFG variables;
	protected final int			Degree	= 1;
	protected IWeightFunction	weight;
	protected double			rho;

	/**
	 * 
	 * @param eParams
	 *            - equation data
	 * @param elNumber
	 *            - element number
	 * @param left
	 *            - left side of element
	 * @param right
	 *            - right side of element
	 * @param numOfMFBaseFunctions
	 *            - number of functions to build
	 * @param degree
	 *            - degree of MF functions
	 * @param weight
	 *            - weight to build MF functions
	 * @param rho
	 *            - parameter to build MF functions
	 */
	public EnrichedLinearElementType2(EquationParameters eParams, int elNumber, final double left, final double right, int numOfMFBaseFunctions, IWeightFunction weight, double rho) {
		super(eParams, elNumber, left, right);

		this.numOfMFBaseFunctions = numOfMFBaseFunctions;
		this.weight = weight;
		this.rho = rho;

//		final double midpoint = (left+right)/2.0;
		final double tmpRight = left + (left+right)/4.0;
		final double tmpLeft = right - (left+right)/4.0;
		baseFunctions = new ArrayList<IFunction1D>();
		IFunction1D N1 = new IFunction1D() {
			@Override
			public double calculate(double x) {
				if (x>tmpRight) {
					return 0.0;
				}
				double xReal = (2 * x - left - tmpRight) / (tmpRight - left);
				return (1 - xReal) / 2.0;
			}

			@Override
			public double calculateDerivaty(double x) {
				if (x>tmpRight) {
					return 0.0;
				}
				return -1.0 / (tmpRight - left);
			}
		};
		baseFunctions.add(N1);

		IFunction1D N2 = new IFunction1D() {
			@Override
			public double calculate(double x) {
				if (x<tmpLeft) {
					return 0.0;
				}
				double xReal = (2 * x - tmpLeft - right) / (right - tmpLeft);
				return (1 + xReal) / 2.0;
			}

			@Override
			public double calculateDerivaty(double x) {
				if (x<tmpLeft) {
					return 0.0;
				}
				return 1.0 / (right - tmpLeft);
			}
		};
		// baseFunctions.add(N2);

		// create MLS funcs;
		// 1 create particles;
		double hEFG = (right - left) / (double) (numOfMFBaseFunctions);
		double rhoH = this.rho * hEFG;
		final double h = (right - left)/4.0;// fem h;
		ArrayList<Particle> ls1D = new ArrayList<Particle>();
		// for (int i = 0; i <= numOfMFBaseFunctions; i++) {
		for (int i = 1; i < numOfMFBaseFunctions; i++) {
			ls1D.add(new Particle(left + hEFG * i, rhoH));
		}
		// 2 create functions
		ArrayList<IFEM1D> fem = new ArrayList<IFEM1D>();
		fem.add(new FEMShapeFunction1D1(left, h));
		fem.add(new FEMShapeFunction1D1(right, h));
		// for (int i = 1; i < ls1D.size() - 1; i++) {
		for (int i = 0; i < ls1D.size(); i++) {
			Particle particle = ls1D.get(i);
			BaseFunctionCoupled jmf1 = new BaseFunctionCoupled(Degree, particle, weight, fem);
			jmf1.setNodeList(ls1D);
			baseFunctions.add(jmf1);
		}
		baseFunctions.add(N2);

	}

	@Override
	public double[] calculate(double x, double[] results) {
		double[] res = new double[3];
		List<Integer> list = this.getNodesNumbers();
		for (int i = 0; i < list.size(); i++) {
			double val = baseFunctions.get(i).calculate(x);
			res[0] += val * results[3 * list.get(i)];
			res[1] += val * results[3 * list.get(i) + 1];
			res[2] += val * results[3 * list.get(i) + 2];
		}
		return res;
		// new double[]{
		// v1*results[3 * elNumber] + v2 * results[3 * (elNumber + 1)],
		// v1*results[3 * elNumber + 1] + v2 * results[3 * (elNumber + 1) + 1],
		// v1*results[3 * elNumber + 2] + v2 * results[3 * (elNumber + 1) + 2]
		// };
	}

	@Override
	protected ElementMatrix calculateSmallData(int i, int j) {
		List<Integer> numbers = new ArrayList<Integer>(2);
		numbers.add(elNumber * (numOfMFBaseFunctions + 1) + i);
		numbers.add(elNumber * (numOfMFBaseFunctions + 1) + j);
		ElementMatrix res = new ElementMatrix(this.getNodesNumbers());

		final IFunction1D fi = baseFunctions.get(i);
		final IFunction1D fj = baseFunctions.get(j);

		MatrixFunction1D mf = new MatrixFunction1D() {

			@Override
			public double[][] calculate(double x) {
				double fiDer = fi.calculateDerivaty(x);
				double fjDer = fj.calculateDerivaty(x);
				double fiVal = fi.calculate(x);
				double fjVal = fj.calculate(x);
				double realXValue = (1 - x) / 2.0 * left + (1 + x) / 2.0 * right;
				double a1 = eParams.getA1().calculate(realXValue);
				double k1 = eParams.getK1().calculate(realXValue);
				return new double[][] {
						// first row
						{ kKk1 / a1 * fiDer * fjDer + a1 * k1 * k1 * kKk2 * fiVal * fjVal, kKk1 / a1 * k1 * fjVal * fiDer - k1 * kKk2 * fjDer * fiVal,
								-a1 * k1 * kKk2 * fiVal * fjVal },
						// second row
						{ -kKk2 * k1 * fjVal * fiDer + k1 * kKk1 * fjDer * fiVal, kKk2 / a1 * fjDer * fiDer + a1 * k1 * k1 * kKk1 * fiVal * fjVal, kKk2 * fjVal * fiDer },
						// third row
						{ -a1 * kKk2 * k1 * fiVal * fjVal, kKk2 * fjDer * fiVal, kKk3 / a1 * fjDer * fiDer + a1 * kKk2 * fiVal * fjVal } };
			}

			@Override
			public double[][] calculateDerivaty(double x) {

				return null;
			}

			@Override
			public int matrixSize() {
				return 3;
			}
		};
		if (fi instanceof HasBaseNode) {
			double midPoint = ((HasBaseNode) fi).getXi().getXi();
			double rho1 = ((HasBaseNode) fi).getXi().getRho();

			res.matrix = gau.integrate(Math.max(left, midPoint - rho1), midPoint, mf);
			double[][] tmp = gau.integrate(midPoint, Math.min(midPoint + rho1, right), mf);
			for (int k = 0; k < tmp.length; k++) {
				for (int k2 = 0; k2 < tmp.length; k2++) {
					res.matrix[k][k2] += tmp[k][k2];
				}
			}
		} else if (fj instanceof HasBaseNode) {
			double midPoint = ((HasBaseNode) fj).getXi().getXi();
			double rho1 = ((HasBaseNode) fj).getXi().getRho();
			res.matrix = gau.integrate(Math.max(left, midPoint - rho1), midPoint, mf);
			double[][] tmp = gau.integrate(midPoint, Math.min(midPoint + rho1, right), mf);
			for (int k = 0; k < tmp.length; k++) {
				for (int k2 = 0; k2 < tmp.length; k2++) {
					res.matrix[k][k2] += tmp[k][k2];
				}
			}
		} else {
			res.matrix = gau.integrate(left, right, mf);
		}

		// right hand side
		res.vector = gau.integrate(left, right, new VectorFunction1D() {

			@Override
			public double[] calculate(double x) {
				double fiVal = fi.calculate(x);
				double h = eParams.getH() / 2.0;
				double realXValue = (1 - x) / 2.0 * left + (1 + x) / 2.0 * right;
				double k1 = eParams.getK1().calculate(realXValue);

				double rho1Val = rho1.calculate(realXValue);
				double rho3Val = rho3.calculate(realXValue);
				double mu1Val = mu1.calculate(realXValue);

				double sigma11Minus = eParams.getSigma11Minus().calculate(realXValue);
				double sigma11Plus = eParams.getSigma11Plus().calculate(realXValue);

				double sigma13Minus = eParams.getSigma13Minus().calculate(realXValue);
				double sigma13Plus = eParams.getSigma13Plus().calculate(realXValue);

				double sigma33Minus = eParams.getSigma33Minus().calculate(realXValue);
				double sigma33Plus = eParams.getSigma33Plus().calculate(realXValue);

				return new double[] { fiVal * ((1 + k1 * h) * sigma13Plus + (1 - k1 * h) * sigma13Minus + rho1Val),
						fiVal * ((1 + k1 * h) * sigma33Plus + (1 - k1 * h) * sigma33Minus + rho3Val),
						fiVal * (((1 + k1 * h) * sigma11Plus - (1 - k1 * h) * sigma11Minus) * h + mu1Val),

				};
			}

			@Override
			public double[] calculateDerivaty(double x) {

				return null;
			}

			@Override
			public int vectorSize() {

				return 3;
			}
		});

		return res;
	}

	@Override
	protected double[] calculateSmallUpdatedVector(int i, final EquationParameters parameters) {
		final IFunction1D fi = baseFunctions.get(i);
		return gau.integrate(left, right, new VectorFunctionWithoutDerivatives1D(3) {

			@Override
			public double[] calculate(double x) {
				double fiVal = fi.calculate(x);
				double h = parameters.getH() / 2.0;
				double realXValue = (1 - x) / 2.0 * left + (1 + x) / 2.0 * right;
				double k1 = parameters.getK1().calculate(realXValue);

				double sigma11Minus = parameters.getSigma11Minus().calculate(realXValue);
				double sigma11Plus = parameters.getSigma11Plus().calculate(realXValue);

				double sigma13Minus = parameters.getSigma13Minus().calculate(realXValue);
				double sigma13Plus = parameters.getSigma13Plus().calculate(realXValue);

				double sigma33Minus = parameters.getSigma33Minus().calculate(realXValue);
				double sigma33Plus = parameters.getSigma33Plus().calculate(realXValue);

				return new double[] { fiVal * ((1 + k1 * h) * sigma13Plus + (1 - k1 * h) * sigma13Minus), fiVal * ((1 + k1 * h) * sigma33Plus + (1 - k1 * h) * sigma33Minus),
						fiVal * (((1 + k1 * h) * sigma11Plus - (1 - k1 * h) * sigma11Minus) * h),

				};
			}
		});
	}

	@Override
	public List<Integer> getNodesNumbers() {
		List<Integer> numbers = new ArrayList<Integer>(2);

		// numbers.add(elNumber * (numOfMFBaseFunctions));
		for (int i = 0; i <= numOfMFBaseFunctions; i++) {
			numbers.add(elNumber + i);
		}
		// numbers.add((elNumber + 1) * (numOfMFBaseFunctions + 1));

		return numbers;
	}

	public static void main(String[] args) {
		double a = 0;
		double b = 1;
		EnrichedLinearElementType2 bbel = new EnrichedLinearElementType2(new EquationParameters(), 0, a, b, 5, new Gaussian(), 2.5);

		List<XYSeries> list = new ArrayList<XYSeries>();
		for (int i = 0; i < bbel.baseFunctions.size(); i++) {
			list.add(new XYSeries("result" + i, true, true));
		}

		int NNN = 150;
		double hhh = (b - a) / NNN;
		for (int k = 0; k <= NNN; k++) {
			double x = a + k * hhh;
			for (int i = 0; i < bbel.baseFunctions.size(); i++) {
//				list.get(i).add(x, bbel.baseFunctions.get(i).calculateDerivaty(x));
				list.get(i).add(x, bbel.baseFunctions.get(i).calculate(x));
			}

		}

		XYSeriesCollection col = new XYSeriesCollection();
		for (Iterator<XYSeries> iterator = list.iterator(); iterator.hasNext();) {
			col.addSeries(iterator.next());
		}
		NewOkCancelDialog d = new NewOkCancelDialog(null, false, col);
		d.setLocationRelativeTo(null);
		d.setVisible(true);
	}

}
